<template>
  <div class="changeUser">
    <div class="addLostTital">修改个人信息</div>
    <div class="name">
      <div class="tishi">
        <span>名字</span>
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-mingpianzhengjian"></use>
        </svg>
      </div>

      <input type="text" v-model="name" />
    </div>

    <button @click="change">提交</button>
    <div class="err" v-if="ifwanzheng">请填写完整</div>
  </div>
</template>

<script>
import { changeName } from "@/api/api.js";

export default {
  data: function () {
    return {
      name: this.$store.state.user.userInfo.name,
      ifwanzheng: false,
    };
  },
  methods: {
    change: async function () {
      //   console.log("提交");
      if (this.name != "") {
        await changeName(this.$route.params.email, this.name);
        let loginOutMes = {
          isLogin: true,
          userInfo: {
            email: this.$route.params.email,
            name: this.name,
          },
        };
        this.$store.commit("setUser", loginOutMes);
        localStorage.user = JSON.stringify(loginOutMes);
        this.$router.push("/user");
      } else {
        this.ifwanzheng = true;
      }
    },
  },
  mounted: async function () {
    // console.log(this.$route.params.email);
  },
  watch:{
    ifwanzheng:function(){
      // console.log(this.ifwanzheng);
      if(this.ifwanzheng){
        let btn = document.querySelector("button")
        btn.style.marginBottom = '0';
      }else{
        let btn = document.querySelector("button")
        btn.style.marginBottom = '1.7rem';
      }
    }
  }
};
</script>

<style lang="less">
.changeUser {
  min-width: 7.5rem;
  min-height: 100vh;
  background-color: #f5f6fa;
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  .addLostTital {
    letter-spacing: 0.1rem;
    padding-left: 0.1rem;
    text-align: justify;
    margin-top: 2rem;
    margin-bottom: 0.2rem;
    font-weight: bold;
    color: #192a56;
  }
  .name {
    display: flex;
    flex-direction: column;
    .tishi {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 0.4rem 0 0.3rem;
      margin-top: 1rem;
      margin-bottom: 0.15rem;
      span {
        color: #273c75;
        letter-spacing: 0.1rem;
      }
    }
    input {
      width: 5rem;
      height: 1rem;
      border: none;
      background-color: #f5f6fa;
      outline: none;
      border: solid 0.04rem #273c75;
      border-radius: 0.5rem;
      padding-left: 0.4rem;
      padding-right: 0.4rem;
      overflow: hidden;
      font-size: 0.4rem;
      color: #273c75;
    }

    .icon {
      width: 0.65rem;
      height: 0.65rem;
      fill: #273c75;
    }
  }
  button {
    width: 4rem;
    height: 1rem;
    border: none;
    border-radius: 0.5rem;
    outline: none;
    background-color: #6851ac;
    color: white;
    font-size: 0.4rem;
    letter-spacing: 0.1rem;
    padding-left: 0.1rem;
    margin-top: 1rem;
    margin-bottom: 1.7rem;
    box-shadow: 0rem 0rem 0.2rem 0.1rem rgba(0, 0, 0, 0.1);
  }
  .err {
    margin-top: 0.2rem;
    color: #bf3030;
    font-size: 0.35rem;
    margin-bottom: 1.5rem;
  }
}
</style>